<?php
namespace app\admin\inter_logic;

use app\admin\inter_model\HotAreaModel;
use app\admin\inter_model\RefundScaleModel;
use app\inter\model\AreaModel;
use think\Db;
use think\exception\ErrorException;

class InterSetUpLogic
{
	public function createArean($data)
	{
		$area_model = new AreaModel();
		return $area_model->addArean($data);
	}

	public function renewArean($map=array(),$data)
	{
		if(empty($data))return false;
		$area_model = new AreaModel();
		return $area_model->updateArean($map,$data);
	}

	public function getAreanList($map,$field='*',$page=1,$limit=10)
	{
		$area_model = new AreaModel();
		return $area_model->getAreaInfo($map,$field,true,$page,$limit);
	}

	public function searchArean($map,$field='*')
	{
		$area_model = new AreaModel();
		return $area_model->searchArean($map,$field);
	}

	public function removeArean($map)
	{
		$area_model = new AreaModel();
		return $area_model->delArean($map);
	}

	//创建地理围栏
	public function createRail($data)
	{
		if(empty($data)||empty($data['id'])||empty($data['rail_local'])){
			return array('status'=>0,'msg'=>'缺少数据');
		}
		$data_res = Db::table('inter_rail')->field('arean_id')->where('id',$data['id'])->find();
        $data_rail['area_id'] = (int)$data_res['arean_id'];
//		$data_rail['area_id'] = (int)$data['id'];

	
		// $res = $this->searchRail($data_rail);
		// if(!empty($res)){
		// 	return array('status'=>0,'msg'=>'围栏数据已存在,请勿重复添加');
		// }
		foreach (unserialize($data['rail_local']) as $key => $val){
			$rail_local = json_decode($val);
			foreach ($rail_local as $k => $v){
				$local[$key][$k][] = $v->lng;
				$local[$key][$k][] = $v->lat;
			}
		}
		foreach ($local as $index => $value){
			$count = count($local[$index]);
			$local[$index][$count] = $local[$index][0];
		}
		$data_rail['polygons']['type'] = "Polygon";
		$data_rail['polygons']['coordinates'] = $local;
		$res = Db::connect(config('mongodb'))->name('rail')->insertGetId($data_rail);
		if($res){
			return array('status'=>1,'msg'=>'围栏数据添加成功','data'=>$res);
		}
		return array('status'=>0,'msg'=>'围栏数据添加失败');
	}

	//更新地理围栏
	public function renewRail($data)
	{
		if(empty($data)||empty($data['id'])||empty($data['rail_local'])){
			return array('status'=>0,'msg'=>'缺少数据');
		}
        $data_res = Db::table('inter_rail')->field('arean_id')->where('id',$data['id'])->find();
        $data_rail['area_id'] = (int)$data_res['arean_id'];
//		$data_rail['area_id'] = (int)$data['id'];
		$data_rail['_id'] = (string)$data['mongo_id'];
		$res = $this->searchRail($data_rail); 
		if(empty($res)){
			return array('status'=>0,'msg'=>'围栏数据不存在，无法更新');
		}
		foreach (unserialize($data['rail_local']) as $key => $val){
			$rail_local = json_decode($val);
			foreach ($rail_local as $k => $v){
				$local[$key][$k][] = $v->lng;
				$local[$key][$k][] = $v->lat;
			}
		}
		foreach ($local as $index => $value){
			$count = count($local[$index]);
			$local[$index][$count] = $local[$index][0];
		}
		$data_rail['polygons']['type'] = "Polygon";
		$data_rail['polygons']['coordinates'] = $local;
		
		$res = Db::connect(config('mongodb'))->name('rail')->where('_id',(string)$data['mongo_id'])->update($data_rail);
		if($res){
			return array('status'=>1,'msg'=>'围栏数据更新成功');
		}
		return array('status'=>0,'msg'=>'围栏数据更新失败');
	}

	//搜索地理围栏
	public function searchRail($map)
	{
		try{
		    $res = Db::connect(config('mongodb'))->name('rail')->where($map)->find();
			return $res;
		}catch (ErrorException $exception){
			return false;
		}
	}

	//删除地理围栏
	public function removeRail($map)
	{
		try{
			$res = Db::connect(config('mongodb'))->name('rail')->where($map)->delete();
			return $res;
		}catch (ErrorException $exception){
			return false;
		}
	}

	public function searchHotAreaMongo($map)
	{
		$res = Db::connect(config('mongodb'))->name('hot_area')->where($map)->find();
		return $res;
	}

	public function createHotArea($data)
	{
		$hot_area_model = new HotAreaModel();
		return $hot_area_model->addHotArea($data);
	}

	public function renewHotArea($map,$data)
	{
		$hot_area_model = new HotAreaModel();
		return $hot_area_model->updateHotArea($map,$data);
	}

	public function getHotArea($map,$field='a.*',$page=1,$limit=10)
	{
		$hot_area_model = new HotAreaModel();
		return $hot_area_model->getHotArea($map,$field,$page,$limit);
	}

	public function searchHotArea($map,$field='*')
	{
		$hot_area_model = new HotAreaModel();
		return $hot_area_model->searchHotArea($map,$field);
	}

	public function removeHotArea($map)
	{
		$area_model = new HotAreaModel();
		return $area_model->delHotArea($map);
	}

	public function createScale($data)
	{
		$scale_model = new RefundScaleModel();
		return $scale_model->addScale($data);
	}

	public function renewScale($map,$data)
	{
		if(empty($data)){
			return false;
		}
		$scale_model = new RefundScaleModel();
		return $scale_model->updateScale($map,$data);
	}

	public function getScaleList($map,$field='a.*',$page=1,$limit=10)
	{
		$scale_model = new RefundScaleModel();
		return $scale_model->getScaleList($map,$field,$page,$limit);
	}

	public function searchScale($map,$field='*')
	{
		$scale_model = new RefundScaleModel();
		return $scale_model->searchScale($map,$field);
	}
}
